﻿Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class PISCOFINS
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'Faz com que ao sair do aplicativo, limpe o buffer e não podendo entrar na página através do butão voltar do Browser.
        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        Try
            'If Not Session("Logged") Then
            '    Session("ShowSessionExpired") = 1 ' Show lblSessionExpired in Login.aspx
            '    Response.Redirect("Login.aspx")
            'End If

            If Not Page.IsPostBack Then

                Dim strConnection As String
                strConnection = "A"

                ' Header
                lblModulo.Text = "Administrador"
                lblModuloItem.Text = "Cadastro PIS/COFINS"

                btnIncluir.Enabled = False
                btnAlterar.Enabled = False
                btnExcluir.Enabled = False

                If (CarregaGVW() = True) Then
                    btnIncluir.Enabled = True
                End If

            End If

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Private Function CarregaGVW() As Boolean

        Dim strWhere As String = ""

        CarregaGVW = False

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String

            theQueryString = "SELECT CodPISCOFINS, PercentualPIS, PercentualCOFINS" & _
                             " FROM piscofins"

            theQueryString = theQueryString & _
                     " ORDER BY CodPISCOFINS DESC"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                gvwData.DataSource = mydatatable
                gvwData.DataBind()

                theSqlDataReader.Close()

                CarregaGVW = True

            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = ex.Message
            End Try

        End Using

    End Function

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        Try
            CarregaGVW()
            gvwData.PageIndex = e.NewPageIndex
            gvwData.DataBind()
        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        Try
            BtnConsultar.Enabled = True
            btnAlterar.Enabled = True
            btnExcluir.Enabled = True

        Catch ex As Exception
            lblMsg.Text = ex.Message
        End Try

    End Sub


    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Incluir

        Session.Add("strCodPISCOFINS", "")
        Session.Add("intPercentualPIS", "")
        Session.Add("intPercentualCOFINS", "")

        Response.Redirect("PISCOFINSData.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodPISCOFINS", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("intPercentualPIS", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("intPercentualCOFINS", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        Response.Redirect("PISCOFINSData.aspx")

    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Session.Add("strOperacao", "E") ' Excluir

        Session.Add("strCodPISCOFINS", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("intPercentualPIS", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("intPercentualCOFINS", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        Response.Redirect("PISCOFINSData.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("Default.aspx")

    End Sub

    Protected Sub BtnConsultar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnConsultar.Click

        Session.Add("strOperacao", "C") ' Consultar

        Session.Add("strCodPISCOFINS", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))
        Session.Add("intPercentualPIS", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(1).Text))
        Session.Add("intPercentualCOFINS", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(2).Text))

        Response.Redirect("PISCOFINSData.aspx")


    End Sub

End Class
